/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997-2009 Sam Lantinga

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Sam Lantinga
    slouken@libsdl.org
*/

#include "SDL_cellvideo.h"

void							CELL_IMAGE_Load				(CELL_OverlayImage* aImage, float aX, float aY, float aWidth, float aHeight, uint32_t* aPixels, uint32_t aImageWidth, uint32_t aImageHeight)
{
	if(aImage->TextureID == 0)
	{
		aImage->TextureID = CELL_PSGL_MakeTexture();
	}

	CELL_PSGL_MakeAttribSetRect(&aImage->VertexBufferID, &aImage->AttributeSetID, aX, aY, aWidth, aHeight);
	glBindTexture(GL_TEXTURE_2D, aImage->TextureID);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_ARGB_SCE, aImageWidth, aImageHeight, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, (void*)aPixels);
}

void							CELL_IMAGE_Free				(CELL_OverlayImage* aImage)
{
	CELL_PSGL_KillAttribSetRect(&aImage->VertexBufferID, &aImage->AttributeSetID);
	glDeleteTextures(1, &aImage->TextureID);
}

void							CELL_IMAGE_Draw				(CELL_OverlayImage* aImage)
{
	glBindTexture(GL_TEXTURE_2D, aImage->TextureID);
	glBindAttribSetSCE(aImage->AttributeSetID);

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glDrawArrays(GL_QUADS, 0, 4);
	glDisable(GL_BLEND);

	glBindAttribSetSCE(0);
}

void							CELL_IMAGE_Draw_T			(CELL_OverlayImage* aImage, int32_t aX, int32_t aY)
{
	glBindTexture(GL_TEXTURE_2D, aImage->TextureID);
	glBindAttribSetSCE(aImage->AttributeSetID);

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	glTranslatef(aX, aY, 0);

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glDrawArrays(GL_QUADS, 0, 4);
	glDisable(GL_BLEND);

	glPopMatrix();

	glBindAttribSetSCE(0);
}

void							CELL_IMAGE_Draw_TA			(CELL_OverlayImage* aImage, int32_t aX, int32_t aY, float aAlpha)
{
	glBindTexture(GL_TEXTURE_2D, aImage->TextureID);
	glBindAttribSetSCE(aImage->AttributeSetID);

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	glTranslatef(aX, aY, 0);

	glEnable(GL_BLEND);
	glBlendFunc(GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA);
	glBlendColor(0, 0, 0, aAlpha);
	glDrawArrays(GL_QUADS, 0, 4);
	glBlendColor(0, 0, 0, 0);
	glDisable(GL_BLEND);

	glPopMatrix();

	glBindAttribSetSCE(0);
}

